


#include <FracturedMesh.h>
#include <Voxelization.h>

#include "Model.h"
#include "KdTree.h"
#include "Aabb.h"
#include <DrawUtils.h>


FracturedMesh::FracturedMesh() : m_time(0.0f)
{
	// Load a model to generate fracture pieces from
	const char* modelFile = "/Users/jamiep/projects/Fracture/Resources/Models/vase03_hires.obj";
	const char* diffuseTextureFile = "/Users/jamiep/projects/Fracture/Resources/Models/vase03.jpg";
	const char* paintedTextureFile = "/Users/jamiep/projects/Fracture/Resources/Models/vase03_color1.jpg";

	m_referenceModel = new Model(modelFile);
	m_referenceModel->loadDiffuseTexture(diffuseTextureFile);
	//m_referenceModel->loadPaintedTexture(paintedTextureFile);
	//m_referenceModel->m_geometry.setVertexWeightFromTexture(m_referenceModel->m_paintedTexture);
	//m_referenceModel->m_geometry.setVertexWeightFromPerlinNoise(200.f, 1.2, 1.5, 10);
	
	//int gridResolution = 64;
	//float weightMargin = 0.05f;
	//int smoothingIterations = 20;
	//m_voxelization = new Voxelization(*m_referenceModel, gridResolution, weightMargin, smoothingIterations);
	
	//m_voxelization->buildFractureGeometries(m_fracturePieceGeometries);
	
	/*
	bool wireFrameRender = false;
	
	for (int i=0; i<m_fracturePieceGeometries.size(); ++i)
	{
		Vec4 axis(float(rand())/float(RAND_MAX), float(rand())/float(RAND_MAX), float(rand())/float(RAND_MAX));
		axis.normalizeIfNotZero();
		m_spinAxes.push_back(axis);
		
		Geometry& geom = m_fracturePieceGeometries[i];
		Aabb aabb = geom.computeAabb();
		m_initialAabbs.push_back(aabb);
		
		geom.compileDisplayList(wireFrameRender);
	}
	 */
}


void FracturedMesh::render(float timestep)
{
	/*
	float period = 0.05f;
	
	float s = 1.0f * fabs(sin(2.f*M_PI*m_time/period));
	float explosionScale = 1.f + 10.f*s;
	float rotationRadians = 0.1*M_PI*s;
	
	bool wireFrameRender = false;
	
	// Explode pieces a little
	for (int i=0; i<m_fracturePieceGeometries.size(); ++i)
	{
		const Aabb& initialAabb = m_initialAabbs[i];
		
		Vec4 center = 0.5f * (initialAabb.m_minima + initialAabb.m_maxima);
		Vec4 newCenter = center * explosionScale;
		Vec4 T = newCenter - center;
		
		Matrix4 transform;
		transform.setTranslation(T);
		transform.setRotation(m_spinAxes[i], rotationRadians);
		
		Geometry& geom = m_fracturePieceGeometries[i];
		geom.setTransform(transform);
	}
	 */
	
	//m_referenceModel->m_diffuseTexture->Apply();
	
	//for (int i=0; i<m_fracturePieceGeometries.size(); ++i)
	//{
	//	Geometry& geom = m_fracturePieceGeometries[i];
		//geom.draw(wireFrameRender);
	//}

	m_referenceModel->debugRender();

	m_time += timestep;
}













